package xyz.playedu.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.playedu.common.domain.AdminRoleCategories;
import xyz.playedu.common.mapper.AdminRoleCategoriesMapper;
import xyz.playedu.common.service.AdminRoleCategoriesService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenguiying
 * @since 2024/7/16 14:59
 */
@Service
public class AdminRoleCategoriesServiceImpl
        extends ServiceImpl<AdminRoleCategoriesMapper, AdminRoleCategories>
        implements AdminRoleCategoriesService {

    @Override
    public List<Integer> getCategoriesByRoleId(Integer roleId) {
        List<AdminRoleCategories> AdminRoleCategoriesList = list(query().getWrapper().eq("role_id", roleId));
        List<Integer> ids = new ArrayList<>();
        for (AdminRoleCategories adminRoleCategories : AdminRoleCategoriesList) {
            ids.add(adminRoleCategories.getCategoriesId());
        }
        return ids;
    }

    @Override
    public void updateByRoleId(Integer roleId, Integer[] perms) {
        QueryWrapper<AdminRoleCategories> wrapper = query().getWrapper().eq("role_id", roleId);
        this.remove(wrapper);
        List<AdminRoleCategories> list = new ArrayList<>();
        for (Integer perm : perms) {
            AdminRoleCategories adminRoleCategories = new AdminRoleCategories();
            adminRoleCategories.setRoleId(roleId);
            adminRoleCategories.setCategoriesId(perm);
            list.add(adminRoleCategories);
        }
        this.saveBatch(list);
    }

    @Override
    public void saveCategories(Integer roleId, Integer[] perms) {
        List<AdminRoleCategories> list = new ArrayList<>();
        for (Integer perm : perms) {
            AdminRoleCategories adminRoleCategories = new AdminRoleCategories();
            adminRoleCategories.setRoleId(roleId);
            adminRoleCategories.setCategoriesId(perm);
            list.add(adminRoleCategories);
        }
        this.saveBatch(list);
    }

}
